var pos = require("Guide");
var nowN = 1;
cc.Class({
    extends: cc.Component,

    properties: {

    },
    getNewNumPosition: function () {
        var Xmin = -250;
        var Xmax = 250;
        var Ymin = -500;
        var Ymax = 400;
        if (pos.length == 0) {
            var randX = parseInt(Math.random() * (Xmax - Xmin + 1) + Xmin, 10);
            var randY = parseInt(Math.random() * (Ymax - Ymin + 1) + Ymin, 10);
            // console.log("NO." + this.game.nowNumber + "  NumX:" + randX + "  NumY:" + randY);
            pos.push(this.game.nowNumber, randX, randY);
            // console.log("[ARR]POS:");
            // console.log(pos);
            return cc.v2(randX, randY);
        }
        else {
            var randX;
            var randY;
            for (let j = 0; j > -1; j++) {
                randX = parseInt(Math.random() * (Xmax - Xmin + 1) + Xmin, 10);
                randY = parseInt(Math.random() * (Ymax - Ymin + 1) + Ymin, 10);
                // console.log("NO." + this.nowNumber + "  NumX:" + randX + "  NumY:" + randY);
                let passed = 0;

                for (let i = 0; i < pos.length; i += 3) {
                    let dis2 = Math.pow(Math.abs(randX - pos[i + 1]), 2) + Math.pow(Math.abs(randY - pos[i + 2]), 2);
                    // console.log("[DIS2]this to " + (i / 3 + 1) + " : " + dis2);
                    // cc.log(pos);
                    if (dis2 < 80000) {
                        // console.log("TOO SHORT!");
                        passed = 1;
                        break;
                    }
                }
                if (passed == 0) {
                    pos.push(this.game.nowNumber, randX, randY);
                    // console.log("NO." + this.game.nowNumber + "  NumX:" + randX + "  NumY:" + randY);
                    // console.log("Right POS:" + randX + "  " + randY);
                    return cc.v2(randX, randY);
                }
            }
        }
    },

    getNewNumPosition2(now) {
        switch (now) {
            case 1:
                return cc.v2(-40, -500)
                break;
            case 2:
                return cc.v2(200, -350)
                break;
            case 3:
                return cc.v2(-200, -350)
                break;
            case 4:
                return cc.v2(-30, -450)
                break;
            case 5:
                return cc.v2(220, -400)
                break;
            case 6:
                return cc.v2(-220, -340)
                break;
            default:
                break;
        }
    },


    randomColor: function () {
        /*
        var colors = [
            '#AD6D6D',
            '#502A2A',
            '#382A50',
            '#66CCFF',
            '#81A250',
            '#F1362F',
            '#2FD2F1',
            '#372FF1',
            '#C8D376',
        ];
        */
        var rgbcolors = [
            173, 109, 109,
            80, 42, 42,
            56, 42, 80,
            74, 104, 179,
            129, 162, 80,
            241, 54, 47,
            102, 139, 139,
            55, 47, 241,
            69, 139, 116,
            181, 165, 142
        ];//30 elements
        var rand = Math.floor(Math.random() * (9 + 1));//0 to 9
        rand *= 3;
        return cc.color(rgbcolors[rand], rgbcolors[rand + 1], rgbcolors[rand + 2]);
    },

    onLoad() {
        this.node.on('touchstart', this.onEventStart, this);
        this.actionflag = false;
    },

    update(dt) {
        if (window.nowNumber == -1) {
            // cc.log("destroy all");
            this.DestroyAll();
        }
    },

    init: function () {
        this.mode = 1;
        this.node.setPosition(this.getNewNumPosition());
        this.node.color = this.randomColor();
        //zoom scale:0.5~0.75
        this.node.scale = parseInt(Math.random() * (7.5 - 4 + 1) + 5, 10) * 0.1;
        this.node.opacity = 0;
        this.node.runAction(cc.fadeIn(0.2));
    },

    init2: function () {
        this.mode = 0;
        this.node.setPosition(this.getNewNumPosition2(nowN));
        nowN++;
        this.node.color = this.randomColor();
        //zoom scale:0.5~0.75
        this.node.scale = parseInt(Math.random() * (6.5 - 4 + 1) + 5, 10) * 0.1;
        this.node.opacity = 0;
        this.node.runAction(cc.fadeIn(0.2));
        // console.log("Loaded");
    },

    onEventStart(event) {
        if (this.mode == 0) //in scene2
            this.judge2();
        else                //in scene 4(training scene)
            this.judge();
    },

    judge: function () {
        var label = this.node.getChildByName('label');
        var thisN = label.getComponent(cc.Label).string;
        if (this.game.score + 1 == thisN) {
            let finished = cc.callFunc(function () {
                this.DestoryThis();
            }, this);
            var zoomin = cc.scaleBy(0.1, 1.1);
            var playDestroyAnime = cc.sequence(cc.fadeOut(0.2), finished);
            this.game.addScore();
            pos.splice(0, 3);
            // console.log(pos);
            this.node.runAction(zoomin);
            this.node.runAction(playDestroyAnime);
            this.game.createNewNumber();
            if (thisN % 10 == 0) {
                window.timerValue += 8;
                this.game.changetimer(window.timerValue, 1);
                // cc.log(window.timerValue);
            }
            console.log("Right!!");
        }
        else {
            var zoomin = cc.scaleBy(0.1, 1.1);
            var zoomout = cc.scaleBy(0.1, 0.9);
            if (this.actionflag == false) {
                this.actionflag = true;
                this.node.runAction(cc.sequence(zoomin, zoomout, cc.callFunc(function () {
                    this.actionflag = false;
                }, this)));
            }
            window.timerValue -= 5;
            if (window.timerValue < 0)
                window.timerValue = 0;
            this.game.changetimer(window.timerValue, 0);
            console.log("Wrong!!");
        }
    },


    judge2: function () {
        var label = this.node.getChildByName('label');
        var thisN = label.getComponent(cc.Label).string;
        if (this.game.score + 1 == thisN) {
            let finished = cc.callFunc(function () {
                this.DestoryThis();
            }, this);
            var zoomin = cc.scaleBy(0.1, 1.1);
            var playDestroyAnime = cc.sequence(cc.fadeOut(0.2), finished);
            this.game.addScore();
            // console.log(pos);
            this.node.runAction(zoomin);
            this.node.runAction(playDestroyAnime);
            this.game.createNewNumber2();

            window.nowNumber++;
            console.log("Right!!");
        }
        else {
            var zoomin = cc.scaleBy(0.1, 1.1);
            var zoomout = cc.scaleBy(0.1, 0.9);
            if (this.actionflag == false) {
                this.actionflag = true;
                this.node.runAction(cc.sequence(zoomin, zoomout, cc.callFunc(function () {
                    this.actionflag = false;
                }, this)));
            }
            console.log("Wrong!!");
        }
    },

    DestroyAll: function () {
        let finished = cc.callFunc(function () {
            this.DestoryThis();
        }, this);
        var playDestroyAnime = cc.sequence(cc.fadeOut(0.2), finished);
        this.node.runAction(playDestroyAnime);
        pos.splice(0, 3);
    },

    DestoryThis: function () {
        cc.removeSelf();
        this.node.destroy();
    },



});
